# 登录接口测试
from httprunner import HttpRunner, Config, Step, RunRequest, RunTestCase
import allure
import csv
import pytest


class TestCaseLogin(HttpRunner):
    config = (
        Config("登录测试")
        .base_url("${read_config()}")
        .verify(False)
        .export(*["token"])
        .variables(**{
            "username": "",
            "password": "",
            "expected_status": 200
        })
    )

    teststeps = [
        Step(
            RunRequest("密码登录")
            .post("/api/user/login")
            .with_json({
                "username": "${username}",
                "password": "${password}"
            })
            .extract()
            .with_jmespath("body.token", "token")
            .validate()
            .assert_equal("status_code", "${expected_status}")
        )
    ]


@allure.epic("管理员登录互帮互助管理系统")
@allure.feature("用户认证模块")
@allure.story("登录接口")
class TestLogin:
    TEST_DATA = [
        {'username': 'admin', 'password': '8i4De25KlVQVwqTDgTNGqw==', 'expected_status': 200},
        {'username': 'xiaogao', 'password': '123456Aa', 'expected_status': 200}
    ]

    @pytest.mark.parametrize("param", TEST_DATA)
    def test_login(self, param):
        # 使用 test_start 方法
        TestCaseLogin().with_variables(**param).test_start()
